package com.myrecycleveiw;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by 黄海 on 2017/4/20.
 */

public class WrapperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "WrapperAdapter";
    RecyclerView.Adapter delegate;
    FooterType footerType;
    ExpandRecycleView expandRecycleView;

    public WrapperAdapter(RecyclerView.Adapter delegate, FooterType footerType, ExpandRecycleView expandRecycleView) {
        this.delegate = delegate;
        this.footerType = footerType;
        this.expandRecycleView = expandRecycleView;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.i(TAG, "onCreateViewHolder() called with: parent = [" + parent + "], viewType = [" + viewType + "]");
        if (viewType == FooterType.Type.LoadMore.getValue()) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loadmore, parent, false);
            return new Holder_LoadMore(inflate);
        } else if (viewType == FooterType.Type.NoMore.getValue()) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nomore, parent, false);
            return new Holder_NoMore(inflate);
        } else if (viewType == FooterType.Type.NetWorkError.getValue()) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_network_error, parent, false);
            return new Holder_NetWorkError(inflate);
        } else
            return delegate.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder() called with: holder = [" + holder + "], position = [" + position + "]");
        if (position + 1 == getItemCount()) {
            if (holder instanceof Holder_NetWorkError) {
                ((Holder_NetWorkError) holder).tvMsg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        footerType.setType(FooterType.Type.LoadMore);
                        notifyItemChanged(getItemCount() - 1);
                        expandRecycleView.onLoadMore();
                    }
                });
            }
        } else {
            delegate.onBindViewHolder(holder, position);
        }
    }

    @Override
    public int getItemCount() {
        return delegate.getItemCount() == 0 ? 0 : delegate.getItemCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        int type = position + 1 == getItemCount() ? footerType.getItemViewType() : delegate.getItemViewType(position);
        Log.e(TAG, "getItemViewType: =" + type);
        return type;
    }

    class Holder_LoadMore extends RecyclerView.ViewHolder {

        public Holder_LoadMore(View itemView) {
            super(itemView);
        }
    }

    class Holder_NoMore extends RecyclerView.ViewHolder {

        public Holder_NoMore(View itemView) {
            super(itemView);
        }
    }

    class Holder_NetWorkError extends RecyclerView.ViewHolder {
        TextView tvMsg;

        public Holder_NetWorkError(View itemView) {
            super(itemView);
            tvMsg = (TextView) itemView.findViewById(R.id.tv_msg);
        }
    }
}
